package com.ruoyi.report_sdj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_sdj.domain.SdjMglblbgBaDetail;
import com.ruoyi.report_sdj.domain.vo.SdjMglblbgBaDetailVo;
import com.ruoyi.report_sdj.mapper.SdjMglblbgBaDetailMapper;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sdj.domain.bo.SdjMglblbgBsInfoBo;
import com.ruoyi.report_sdj.domain.vo.SdjMglblbgBsInfoVo;
import com.ruoyi.report_sdj.domain.SdjMglblbgBsInfo;
import com.ruoyi.report_sdj.mapper.SdjMglblbgBsInfoMapper;
import com.ruoyi.report_sdj.service.ISdjMglblbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 锚杆拉拔力试验检测报告
Service业务层处理
 *
 * @author zyb
 * @date 2024-07-23
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class SdjMglblbgBsInfoServiceImpl implements ISdjMglblbgBsInfoService {

    private final SdjMglblbgBsInfoMapper baseMapper;
    private final SdjMglblbgBaDetailMapper detailMapper;

    /**
     * 查询锚杆拉拔力试验检测报
     */
    @Override
    @FIllBasicInfo
    public SdjMglblbgBsInfoVo queryById(Long excelId){
        SdjMglblbgBsInfoVo sdjMglblbgBsInfoVo = baseMapper.selectVoById(excelId);
        List<SdjMglblbgBaDetailVo> detailVos = detailMapper.selectVoList(new LambdaQueryWrapper<SdjMglblbgBaDetail>()
            .eq(SdjMglblbgBaDetail::getExcelId, excelId)
            .orderByAsc(SdjMglblbgBaDetail::getXuh));
        if (CollUtil.isNotEmpty(detailVos)) {
            sdjMglblbgBsInfoVo.setBaDetailVos(detailVos);
        }
        // 如果已经有值了则不需要计算结论。
        if (StrUtil.isNotEmpty(sdjMglblbgBsInfoVo.getJiancjl()) && !"检测结论：".equals(sdjMglblbgBsInfoVo.getJiancjl())) {
            return sdjMglblbgBsInfoVo;
        }
        return jcjl(sdjMglblbgBsInfoVo);
    }

    /**
     * 查询锚杆拉拔力试验检测报告列表
     */
    @Override
    public TableDataInfo<SdjMglblbgBsInfoVo> queryPageList(SdjMglblbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SdjMglblbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<SdjMglblbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询锚杆拉拔力试验检测报告列表
     */
    @Override
    public List<SdjMglblbgBsInfoVo> queryList(SdjMglblbgBsInfoBo bo) {
        LambdaQueryWrapper<SdjMglblbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SdjMglblbgBsInfo> buildQueryWrapper(SdjMglblbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SdjMglblbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, SdjMglblbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, SdjMglblbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), SdjMglblbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), SdjMglblbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), SdjMglblbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), SdjMglblbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), SdjMglblbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), SdjMglblbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), SdjMglblbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), SdjMglblbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancrq()), SdjMglblbgBsInfo::getJiancrq, bo.getJiancrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), SdjMglblbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), SdjMglblbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), SdjMglblbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), SdjMglblbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), SdjMglblbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getQuywz()), SdjMglblbgBsInfo::getQuywz, bo.getQuywz());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitrwbh()), SdjMglblbgBsInfo::getWeitrwbh, bo.getWeitrwbh());
        lqw.eq(StringUtils.isNotBlank(bo.getTuycd()), SdjMglblbgBsInfo::getTuycd, bo.getTuycd());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), SdjMglblbgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), SdjMglblbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), SdjMglblbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), SdjMglblbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), SdjMglblbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), SdjMglblbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), SdjMglblbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), SdjMglblbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), SdjMglblbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getMaoglx()), SdjMglblbgBsInfo::getMaoglx, bo.getMaoglx());
        return lqw;
    }

    /**
     * 新增锚杆拉拔力试验检测报告
     */
    @Override
    public Boolean insertByBo(SdjMglblbgBsInfoBo bo) {
        SdjMglblbgBsInfo add = BeanUtil.toBean(bo, SdjMglblbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 18
            ArrayList<SdjMglblbgBaDetail> details = new ArrayList<>(18);
            for (long i = 1; i <= 18; i++) {
                details.add(new SdjMglblbgBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改锚杆拉拔力试验检测报告
     */
    @Override
    @ComputeReportResult
    public Boolean updateByBo(SdjMglblbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        SdjMglblbgBsInfo update = BeanUtil.toBean(bo, SdjMglblbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), SdjMglblbgBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(SdjMglblbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (SdjMglblbgBaDetailVo detailVo : bo.getBaDetailVos()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SdjMglblbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除锚杆拉拔力试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 生成 锚杆拉拔力试验 报告 检测结论
     */
    @Override
    public SdjMglblbgBsInfoVo jcjl(SdjMglblbgBsInfoVo vo) {
//        if (StrUtil.isEmpty(vo.getPandyj())) {
//            return vo;
//        }
        vo.setJiancjl("检测结论：经检测，该初期支护中空锚杆的所检参数符合" + "JTG F80/1-2017" + "及设计文件要求。");
        return vo;
    }

}
